
import { deleteAdminApiLicenseApi, postAdminApiLicenseUnbindApi } from '@/apis/modules/authBind';
import {
  Form,
  FormItem,
  Input,
  Button
} from '@arco-design/web-vue'

export default function useCommand(reload: () => void) {
  const router = useRouter()
  const handleCommand = (
    command: string,
    ids: string[],
  ) => {
    const map: Record<string, () => void> = {
      unbind: () => {
        const fn = () => {
          postAdminApiLicenseUnbindApi({ id: ids[0] as string }).then(
            () => {
              ToastUtils.success('操作成功')
              reload()
            }
          )
        }

        // 解绑机器码
        CloudModal.error(
          {
            maskClosable: false,
            content: '解绑授权，确认继续？'
          },
          fn
        )
      },
      delete: () => {
        const nameVerification = reactive({ name: '' })
        const disabled = computed(() => nameVerification.name !== '立即删除')
        const loading = ref(false)
        // 删除授权
        const fn = () => {
          loading.value = true
          deleteAdminApiLicenseApi({ id: ids[0] as string })
            .then(() => {
              ToastUtils.success('操作成功')
              modalReturn.close()
              router.back()
            })
            .finally(() => loading.value = false)
        }

        const modalReturn = CloudModal.error(
          {
            maskClosable: false,
            okText: '确定',
            footer: () => {
              return (
                <div class="flex  justify-end">
                  {/* <Button onClick={() => modalReturn.close()} class="mr-4">
                    取消
                  </Button> */}
                  <Button
                    status="danger"
                    disabled={disabled.value}
                    class={disabled.value ? 'bg-[#FBACA3]! w-full' : 'w-full'}
                    style={disabled.value ? 'border:1px solid #FBACA3' : ''}
                    onClick={() => fn()}
                    loading={loading.value}>
                    我已知晓后果并确认删除
                  </Button>
                </div>
              )
            },
            content: () =>
            (
              <div class="flex flex-col gap-y-6 text-1 w-full">
                <span class={`text-[#1D2129] `}>
                  确认要删除授权吗？此操作无法撤销。
                </span>
                <div class="w-full">
                  <Form layout="vertical" model={nameVerification}>
                    <FormItem
                      asteriskPosition="end"
                      field="name"
                      validateTrigger={'blur'}
                      hide-asterisk
                    // rules={[
                    //   {
                    //     required: true,
                    //     validator(value, cb) {
                    //       if (!value) {
                    //         return cb('请输入提示内容')
                    //       }
                    //       if (value !== row?.name) {
                    //         return cb('请输入提示内容')
                    //       }
                    //       return cb()
                    //     }
                    //   }
                    // ]}
                    >
                      {{
                        label: () => (
                          <span class="text-4">
                            请输入「
                            <span class={'text-red-500'}>立即删除</span>
                            」确认
                          </span>
                        ),
                        default: () => (
                          <Input
                            class="w-full"
                            modelValue={nameVerification.name}
                            maxLength={64}
                            onInput={(v) => {
                              nameVerification.name = v
                            }}
                            placeholder=""></Input>
                        )
                      }}
                    </FormItem>
                  </Form>
                </div>
              </div>
            )
          },
          fn
        )
      },
    }
    map[command]()
  }
  return { handleCommand }
}
